package cc.leap.service;

import cc.leap.mapper.SysDictDataMapper;
import cc.leap.model.PageQuery;
import cc.leap.model.TableDataInfo;
import cc.leap.model.domain.SysDictData;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

import static cc.leap.model.domain.table.SysDictDataTableDef.SYS_DICT_DATA;

/**
 * SysDictDataService
 *
 * @author leap
 * @date 2024/8/20
 */
@Component
public class SysDictDataService extends ServiceImpl<SysDictDataMapper, SysDictData> {

    @Inject
    SysDictDataMapper dictDataMapper;

    /**
     * 构造查询条件
     *
     * @param dictDataBo
     * @return QueryWrapper
     */
    private QueryWrapper buildQueryWrapper(SysDictData dictDataBo) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SYS_DICT_DATA.DICT_CODE, SYS_DICT_DATA.DICT_SORT, SYS_DICT_DATA.DICT_LABEL, SYS_DICT_DATA.DICT_VALUE, SYS_DICT_DATA.DICT_TYPE, SYS_DICT_DATA.CSS_CLASS, SYS_DICT_DATA.LIST_CLASS, SYS_DICT_DATA.IS_DEFAULT, SYS_DICT_DATA.STATUS, SYS_DICT_DATA.CREATE_BY, SYS_DICT_DATA.CREATE_TIME, SYS_DICT_DATA.REMARK)
                .and(SYS_DICT_DATA.DICT_TYPE.eq(dictDataBo.getDictType()))
                .and(SYS_DICT_DATA.DICT_LABEL.eq(dictDataBo.getDictLabel()))
                .orderBy(SYS_DICT_DATA.DICT_SORT.asc());
        return queryWrapper;
    }

    public TableDataInfo<SysDictData> selectDictDataPage(SysDictData dictData) {
        QueryWrapper queryWrapper = buildQueryWrapper(dictData);
        Page<SysDictData> page = this.pageAs(PageQuery.build(), queryWrapper, SysDictData.class);
        return TableDataInfo.build(page);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    public SysDictData selectDictDataById(Long dictCode) {
       return dictDataMapper.selectDictDataById(dictCode);
    }

    public int insertDictData(SysDictData data) {
        int row = dictDataMapper.insertDictData(data);
        //可以加缓存
        return row;
    }

    public int updateDictData(SysDictData data) {
        int row = dictDataMapper.updateDictData(data);
        //可以加缓存
        return row;
    }

}
